package cn.iocoder.yudao.module.system.controller.app.investment;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.InvestmentReqVO;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.InvestmentRespVO;
import cn.iocoder.yudao.module.system.controller.app.investment.vo.AppInvestmentPageReqVO;
import cn.iocoder.yudao.module.system.controller.app.investment.vo.AppInvestmentPageRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.investment.SystemInvestment;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.MerchantDO;
import cn.iocoder.yudao.module.system.service.investment.InvestmentService;
import cn.iocoder.yudao.module.system.service.merchant.MerchantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wzy
 */
@Tag(name = "用户App - 招商页")
@RestController
@RequestMapping("/user/investment")
@Validated
public class AppInvestmentController {
    @Resource
    private InvestmentService investmentService;
    @Resource
    private MerchantService merchantService;

    @GetMapping("/page")
    @Operation(summary = "搜索招商项目")
    public CommonResult<PageResult<AppInvestmentPageRespVO>> getInvestmentPage(@Valid AppInvestmentPageReqVO pageVO) {
        // 从 service 获取分页结果，类型为 SystemInvestment
        PageResult<SystemInvestment> pageResult = investmentService.getInvestmentPageResult(pageVO);

        // 将 SystemInvestment 转换为 InvestmentRespVO
        List<AppInvestmentPageRespVO> voList = BeanUtils.toBean(pageResult.getList(), AppInvestmentPageRespVO.class);

        // 构造新的分页结果
        PageResult<AppInvestmentPageRespVO> voPageResult = new PageResult<>(voList, pageResult.getTotal());

        // 返回成功的分页结果
        return CommonResult.success(voPageResult);
    }


    @GetMapping("/get-investInfo")
    @Operation(summary = "获取招商项目详情")
    public CommonResult<InvestmentRespVO> getInvestmentDetail(@RequestParam Long id) {
        // 获取指定 ID 的 SystemInvestment
        SystemInvestment systemInvestment = investmentService.getInvestmentInfo(id);

        InvestmentRespVO respVO = new InvestmentRespVO();
        // 将基本属性复制到 InvestmentRespVO
        respVO.setId(systemInvestment.getId());
        respVO.setName(systemInvestment.getName());
        respVO.setMerchantId(systemInvestment.getMerchantId());
        respVO.setDescription(systemInvestment.getDescription());
        respVO.setDescriptionUrl(systemInvestment.getDescriptionUrl());
        respVO.setContactPerson(systemInvestment.getContactPerson());
        respVO.setContactEmail(systemInvestment.getContactEmail());
        respVO.setContactPhone(systemInvestment.getContactPhone());
        respVO.setStartDate(systemInvestment.getStartDate());
        respVO.setEndDate(systemInvestment.getEndDate());

        // 根据 merchantId 查询商户信息
        Long merchantId = systemInvestment.getMerchantId();
        if (merchantId != null) {
            MerchantDO merchant = merchantService.getMerchant(merchantId);
            if (merchant != null) {
                respVO.setMerchantName(merchant.getMerchantName());
                respVO.setContactAddress(merchant.getContactAddress());
            }
        }

        // 返回成功的招商项目详情
        return CommonResult.success(respVO);
    }




}
